/*
 * testresmanager.h
 * This file is part of OpenAnimator
 *
 * Copyright (C) 2008 - Abhinav Lele
 *
 * OpenAnimator is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * OpenAnimator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenAnimator; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */
#ifndef __OA_RESMANAGERTEST_H__
#define __OA_RESMANAGERTEST_H__
#include "commoninc.h"
#include <iostream>
#include <time.h> 

#include "SDL.h"
#include "SDL_opengl.h"

using std::cout;

struct ResManagerFixture {
    ResManager myMan; //only for the dummy test
    ResManagerFixture(){ 
        cout << "ResManagerFixture()\n" ;
        RenderInit();
    }
    ~ResManagerFixture() {
        cout << "~ResManagerFixture()\n";
    }
    void RenderInit()  {
		const SDL_VideoInfo* info = NULL;
		//SDL_Event event;
		putenv("SDL_VIDEO_WINDOW_POS");
		putenv("SDL_VIDEO_CENTERED=1");

		getenv("SDL_VIDEO_WINDOW_POS");
		getenv("SDL_VIDEO_CENTERED");
		cout << "RendererTestFixture: Init SDL ";
		if(SDL_Init(SDL_INIT_EVERYTHING)==-1) { cout << "[ERR] SDL video init failed!\n"; return; }
		else cout << "Success!\n";

		int w = 400, h=400;

		/* Check for width & height settings if set */
		map< string, generic_object, ltstr_stl>::iterator tempiter = myConfig.repository.find("config");
		map< string, generic_data, ltstr_stl >::iterator   tempiter_string;
		generic_object *ptr; generic_data* dataptr;
		if(tempiter != myConfig.repository.end()) {
			ptr = (& tempiter->second );
			tempiter_string = ptr->stringStore.find("window-width");
			if( tempiter_string != ptr->stringStore.end() ) { dataptr = & tempiter_string->second; if(dataptr->isNumber) w = dataptr->store.fData;  }

			tempiter_string = ptr->stringStore.find("window-height");
			if( tempiter_string != ptr->stringStore.end() ) { dataptr = & tempiter_string->second; if(dataptr->isNumber) h = dataptr->store.fData;  }
		}


		cout << "[INFO] w = " << w << " h = " << h << endl;

		info = SDL_GetVideoInfo();
		if(!info) { cout << "[ERR]SDL video query fail ! "; return; }

		SDL_GL_SetAttribute(SDL_GL_RED_SIZE,8);
		SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,8);
		SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,8);
		SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,16);
		SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
		SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,8);

		//SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,1);
		//SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,1);
		if(SDL_SetVideoMode(w,h,0/*info->vfmt->BitsPerPixel*/, SDL_OPENGL | SDL_RESIZABLE | SDL_DOUBLEBUF)==0) { cout << "[ERR] SDL opengl set video mode fail !\n"; return ; }
				
		gl_resize(w,h);
		glEnable(GL_LINE_SMOOTH);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

	    if(SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1) == -1) { cout << "[ERR] setting vsync failed\n"; }
		else { cout << "[INFO] Render sync set\n"; }
			glEnable(GL_TEXTURE_2D);
//    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	}

	void gl_resize(int w,int h) {
		glClearColor(0,0,0,0);
		glViewport(0,0,(GLsizei) w , (GLsizei) h );
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();

		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();

//	    glMatrixMode( GL_MODELVIEW );
//	    glLoadIdentity( );

		gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
		return;
			int width =w ,height=h;
    GLfloat ratio;
 
    /* Protect against a divide by zero */
    if ( height == 0 )
	height = 1;

    ratio = ( GLfloat )width / ( GLfloat )height;

    /* Setup our viewport. */
    glViewport( 0, 0, ( GLint )width, ( GLint )height );

    /*
     * change to the projection matrix and set
     * our viewing volume.
     */
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity( );

    /* Set our perspective */
    gluPerspective( 45.0f, ratio, 0.1f, 100.0f );

    /* Make sure we're chaning the model view and not the projection */
    glMatrixMode( GL_MODELVIEW );

    /* Reset The View */
    glLoadIdentity( );

		gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
		
		
	}
    
};

#endif

